package com.bootdo.wx.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.wx.domain.MemberSearchDO;
import com.bootdo.wx.service.MemberSearchService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 搜索列表
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2021-05-08 13:46:20
 */
 
@Controller
@RequestMapping("/wx/memberSearch")
public class MemberSearchController {
	@Autowired
	private MemberSearchService memberSearchService;
	
	@GetMapping()
	@RequiresPermissions("wx:memberSearch:memberSearch")
	String MemberSearch(){
	    return "wx/memberSearch/memberSearch";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:memberSearch:memberSearch")
	public PageUtils list(@RequestParam Map<String, Object> params){

	    params.put("sort","create_at");
	    params.put("order","desc");
	    params.put("type","0");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<MemberSearchDO> memberSearchList = memberSearchService.likeList(query);
            int total = memberSearchList.size();
            PageUtils pageUtils = new PageUtils(memberSearchList, total);
            return pageUtils;
		}

		List<MemberSearchDO> memberSearchList = memberSearchService.list(query);
		int total = memberSearchService.count(query);
		PageUtils pageUtils = new PageUtils(memberSearchList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:memberSearch:add")
	String add(){
	    return "wx/memberSearch/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:memberSearch:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MemberSearchDO memberSearch = memberSearchService.get(id);
		model.addAttribute("memberSearch", memberSearch);
	    return "wx/memberSearch/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:memberSearch:add")
	public R save( MemberSearchDO memberSearch){
		memberSearch.setType(0);
		memberSearch.setNum(0);
		memberSearch.setCreateAt(new Date());
		if(memberSearchService.save(memberSearch)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:memberSearch:edit")
	public R update( MemberSearchDO memberSearch){

		memberSearchService.update(memberSearch);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:memberSearch:remove")
	public R remove( Long id){
		memberSearchService.remove(id);

		return R.ok();

	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:memberSearch:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids) {

		memberSearchService.batchRemove(ids);
		return R.ok();

	}
}
